<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>CheckLibraryExists &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="CheckLinkerFlag" href="CheckLinkerFlag.html" />
    <link rel="prev" title="CheckLanguage" href="CheckLanguage.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="CheckLinkerFlag.html" title="CheckLinkerFlag"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="CheckLanguage.html" title="CheckLanguage"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CheckLibraryExists</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="checklibraryexists">
<span id="module:CheckLibraryExists"></span><h1>CheckLibraryExists<a class="headerlink" href="#checklibraryexists" title="Link to this heading">¶</a></h1>
<p>This module provides a command to check whether a C library exists.</p>
<p>Load this module in a CMake project with:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CheckLibraryExists</span><span class="nf">)</span>
</pre></div>
</div>
<section id="commands">
<h2>Commands<a class="headerlink" href="#commands" title="Link to this heading">¶</a></h2>
<p>This module provides the following command:</p>
<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:check_library_exists">
<span class="sig-name descname"><span class="pre">check_library_exists</span></span><a class="headerlink" href="#command:check_library_exists" title="Link to this definition">¶</a></dt>
<dd><p>Checks once whether a specified library exists and a given C function is
available:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">check_library_exists(</span><span class="nv">&lt;library&gt;</span><span class="w"> </span><span class="nv">&lt;function&gt;</span><span class="w"> </span><span class="nv">&lt;location&gt;</span><span class="w"> </span><span class="nv">&lt;variable&gt;</span><span class="nf">)</span>
</pre></div>
</div>
<p>This command attempts to link a test executable that uses the specified
C <code class="docutils literal notranslate"><span class="pre">&lt;function&gt;</span></code> to verify that it is provided by either a system or
user-provided <code class="docutils literal notranslate"><span class="pre">&lt;library&gt;</span></code>.</p>
<p>The arguments are:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">&lt;library&gt;</span></code></dt><dd><p>The name of the library, a full path to a library file, or an
<a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">Imported Target</span></a>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">&lt;function&gt;</span></code></dt><dd><p>The name of a function that should be available in the system or
user-provided library <code class="docutils literal notranslate"><span class="pre">&lt;library&gt;</span></code>.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">&lt;location&gt;</span></code></dt><dd><p>The directory containing the library file.  It is added to the link
search path during the check.  If this is an empty string, only the
default library search paths are used.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">&lt;variable&gt;</span></code></dt><dd><p>The name of the variable in which to store the check result.  This
variable will be created as an internal cache variable.</p>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This command is intended for performing basic sanity checks to verify
that a library provides the expected functionality, or that the correct
library is being located.  However, it only verifies that a function
symbol can be linked successfully - it does not ensure that the function
is declared in library headers, nor can it detect functions that are
inlined or defined as preprocessor macros.  For more robust detection
of function availability, prefer using <span class="target" id="index-0-module:CheckSymbolExists"></span><a class="reference internal" href="CheckSymbolExists.html#module:CheckSymbolExists" title="CheckSymbolExists"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">CheckSymbolExists</span></code></a> or
<span class="target" id="index-0-module:CheckSourceCompiles"></span><a class="reference internal" href="CheckSourceCompiles.html#module:CheckSourceCompiles" title="CheckSourceCompiles"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">CheckSourceCompiles</span></code></a>.</p>
</div>
<p class="rubric">Variables Affecting the Check</p>
<p>The following variables may be set before calling this command to modify
the way the check is run:</p>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_FLAGS</span></code></dt><dd><p>A space-separated string of additional flags to pass to the compiler.
A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> will not work.
The contents of <span class="target" id="index-0-variable:CMAKE_&lt;LANG&gt;_FLAGS"></span><a class="reference internal" href="../variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_&lt;LANG&gt;_FLAGS" title="CMAKE_&lt;LANG&gt;_FLAGS"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_&lt;LANG&gt;_FLAGS</span></code></a> and its associated
configuration-specific <span class="target" id="index-0-variable:CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;"></span><a class="reference internal" href="../variable/CMAKE_LANG_FLAGS_CONFIG.html#variable:CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;" title="CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;"><code class="xref cmake cmake-variable docutils literal notranslate"><span class="pre">CMAKE_&lt;LANG&gt;_FLAGS_&lt;CONFIG&gt;</span></code></a> variables
are automatically prepended to the compiler command before the contents of
this variable.</p>
</dd>
</dl>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_DEFINITIONS</span></code></dt><dd><p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of compiler
definitions, each of the form <code class="docutils literal notranslate"><span class="pre">-DFOO</span></code> or <code class="docutils literal notranslate"><span class="pre">-DFOO=bar</span></code>.  A definition for
the name specified by the result variable argument of the check
command is also added automatically.</p>
</dd>
</dl>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_LINK_OPTIONS</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.14.</span></p>
</div>
<p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of options to
add to the link command (see <span class="target" id="index-0-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> for further details).</p>
</dd>
</dl>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_LIBRARIES</span></code></dt><dd><p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of libraries to
add to the link command.  These can be the names of system libraries, or
they can be <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">Imported Targets</span></a> (see <span class="target" id="index-1-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> for further
details).</p>
</dd>
</dl>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_LINK_DIRECTORIES</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.31.</span></p>
</div>
<p>A <a class="reference internal" href="../manual/cmake-language.7.html#cmake-language-lists"><span class="std std-ref">semicolon-separated list</span></a> of library search
paths to pass to the linker (see <span class="target" id="index-2-command:try_compile"></span><a class="reference internal" href="../command/try_compile.html#command:try_compile" title="try_compile"><code class="xref cmake cmake-command docutils literal notranslate"><span class="pre">try_compile()</span></code></a> for further
details).</p>
</dd>
</dl>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_REQUIRED_QUIET</span></code></dt><dd><div class="versionadded">
<p><span class="versionmodified added">Added in version 3.1.</span></p>
</div>
<p>If this variable evaluates to a boolean true value, all status messages
associated with the check will be suppressed.</p>
</dd>
</dl>
</dd></dl>

</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>Checking if the <code class="docutils literal notranslate"><span class="pre">curl</span></code> library exists in the default paths and has the
<code class="docutils literal notranslate"><span class="pre">curl_easy_perform()</span></code> function:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CheckLibraryExists</span><span class="nf">)</span>
<span class="nf">check_library_exists(</span><span class="nb">curl</span><span class="w"> </span><span class="nb">curl_easy_perform</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="w"> </span><span class="no">HAVE_LIBRARY_CURL</span><span class="nf">)</span>
</pre></div>
</div>
<p>To check if library exists in specific non-standard location and has a specified
function:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">include(</span><span class="nb">CheckLibraryExists</span><span class="nf">)</span>
<span class="nf">check_library_exists(</span><span class="nb">curl</span><span class="w"> </span><span class="nb">curl_easy_perform</span><span class="w"> </span><span class="s">&quot;/opt/curl/lib&quot;</span><span class="w"> </span><span class="no">HAVE_LIBRARY_CURL</span><span class="nf">)</span>
</pre></div>
</div>
<p>Also <a class="reference internal" href="../manual/cmake-buildsystem.7.html#imported-targets"><span class="std std-ref">Imported Targets</span></a> (for example, from the <code class="docutils literal notranslate"><span class="pre">find_package()</span></code> call)
can be used:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">find_package(</span><span class="no">CURL</span><span class="nf">)</span>

<span class="c"># ...</span>

<span class="nf">if(</span><span class="no">TARGET</span><span class="w"> </span><span class="no">CURL</span><span class="o">::</span><span class="nb">libcurl</span><span class="nf">)</span>
<span class="w">  </span><span class="nf">include(</span><span class="nb">CheckLibraryExists</span><span class="nf">)</span>
<span class="w">  </span><span class="nf">check_library_exists(</span><span class="no">CURL</span><span class="o">::</span><span class="nb">libcurl</span><span class="w"> </span><span class="nb">curl_easy_perform</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="w"> </span><span class="no">HAVE_LIBRARY_CURL</span><span class="nf">)</span>
<span class="nf">endif()</span>
</pre></div>
</div>
</section>
<section id="see-also">
<h2>See Also<a class="headerlink" href="#see-also" title="Link to this heading">¶</a></h2>
<ul class="simple">
<li><p>The <span class="target" id="index-1-module:CheckSymbolExists"></span><a class="reference internal" href="CheckSymbolExists.html#module:CheckSymbolExists" title="CheckSymbolExists"><code class="xref cmake cmake-module docutils literal notranslate"><span class="pre">CheckSymbolExists</span></code></a> module to check whether a C symbol exists.</p></li>
</ul>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">CheckLibraryExists</a><ul>
<li><a class="reference internal" href="#commands">Commands</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#see-also">See Also</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="CheckLanguage.html"
                          title="previous chapter">CheckLanguage</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="CheckLinkerFlag.html"
                          title="next chapter">CheckLinkerFlag</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/CheckLibraryExists.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="CheckLinkerFlag.html" title="CheckLinkerFlag"
             >next</a> |</li>
        <li class="right" >
          <a href="CheckLanguage.html" title="CheckLanguage"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CheckLibraryExists</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>